package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

/**
 * 教师仪表板数据传输对象
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "教师仪表板数据")
public class TeacherDashboardDTO {

    @NotNull(message = "工具列表不能为空")
    @Schema(description = "教学工具列表", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<ToolDTO> tools;

    @NotNull(message = "资源列表不能为空")
    @Schema(description = "教学资源列表", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<ResourceDTO> resources;

    @NotNull(message = "课堂活动列表不能为空")
    @Schema(description = "课堂活动列表", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<ClassActivityDTO> classActivities;

    @NotNull(message = "证书列表不能为空")
    @Schema(description = "证书列表", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<CertificateDTO> certificates;

    @Schema(description = "教师ID", example = "T2024001")
    private String teacherId;

    @Schema(description = "教师姓名", example = "张老师")
    private String teacherName;

    @Schema(description = "所教班级", example = "一年级一班, 一年级二班")
    private String teachingClasses;

    @Schema(description = "统计数据")
    private TeacherStatsDTO stats;

    /**
     * 获取资源总数
     */
    @Schema(description = "资源总数", accessMode = Schema.AccessMode.READ_ONLY)
    public int getTotalResources() {
        return resources != null ? resources.size() : 0;
    }

    /**
     * 获取进行中的活动
     */
    @Schema(description = "进行中的活动数量", accessMode = Schema.AccessMode.READ_ONLY)
    public long getOngoingActivities() {
        if (classActivities == null) return 0;
        return classActivities.stream()
                .filter(activity -> "ongoing".equals(activity.getStatus()))
                .count();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "教学工具信息")
    public static class ToolDTO {

        @NotBlank(message = "工具名称不能为空")
        @Schema(description = "工具名称", example = "课程管理", requiredMode = Schema.RequiredMode.REQUIRED)
        private String name;

        @NotBlank(message = "工具图标不能为空")
        @Schema(description = "Element Plus图标名称", example = "Clock", requiredMode = Schema.RequiredMode.REQUIRED)
        private String icon;

        @Schema(description = "工具描述", example = "管理课程安排和内容")
        private String description;

        @Schema(description = "路由路径", example = "/course-management")
        private String route;

        @Schema(description = "是否常用", example = "true")
        @Builder.Default
        private Boolean frequentlyUsed = true;

        /**
         * 获取工具标识
         */
        @Schema(description = "工具标识", accessMode = Schema.AccessMode.READ_ONLY)
        public String getToolKey() {
            return name.toLowerCase().replace(" ", "-");
        }
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "教学资源信息")
    public static class ResourceDTO {

        @NotNull(message = "资源ID不能为空")
        @Positive(message = "资源ID必须是正整数")
        @Schema(description = "资源唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long id;

        @NotBlank(message = "资源标题不能为空")
        @Schema(description = "资源标题", example = "教学课件", requiredMode = Schema.RequiredMode.REQUIRED)
        private String title;

        @NotBlank(message = "资源类型不能为空")
        @Schema(description = "资源类型", example = "pdf", allowableValues = {"pdf", "word", "video", "audio", "image"}, requiredMode = Schema.RequiredMode.REQUIRED)
        private String type;

        @JsonFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "更新时间", example = "2023-10-15", requiredMode = Schema.RequiredMode.REQUIRED)
        private String updatedAt;

        @NotBlank(message = "资源分类不能为空")
        @Schema(description = "资源分类", example = "class", allowableValues = {"class", "subscription"}, requiredMode = Schema.RequiredMode.REQUIRED)
        private String category;

        @Schema(description = "资源URL", example = "/resources/1/download")
        private String url;

        @Schema(description = "文件类型", example = "application/pdf")
        private String fileType;

        @Schema(description = "使用次数", example = "45")
        private Integer usageCount;

        @Schema(description = "分享次数", example = "12")
        private Integer shareCount;

        /**
         * 获取文件图标
         */
        @Schema(description = "文件图标", accessMode = Schema.AccessMode.READ_ONLY)
        public String getFileIcon() {
            return switch (type.toLowerCase()) {
                case "pdf" -> "Document";
                case "word" -> "Document";
                case "video" -> "VideoPlay";
                case "audio" -> "Headphone";
                case "image" -> "Picture";
                default -> "QuestionFilled";
            };
        }
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "课堂活动信息")
    public static class ClassActivityDTO {

        @NotNull(message = "活动ID不能为空")
        @Positive(message = "活动ID必须是正整数")
        @Schema(description = "活动唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long id;

        @NotBlank(message = "活动标题不能为空")
        @Schema(description = "活动标题", example = "数学课堂练习", requiredMode = Schema.RequiredMode.REQUIRED)
        private String title;

        @NotBlank(message = "活动类型不能为空")
        @Schema(description = "活动类型", example = "participated", allowableValues = {"participated", "reviewed"}, requiredMode = Schema.RequiredMode.REQUIRED)
        private String type;

        @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
        @Schema(description = "活动时间", example = "2023-11-05 14:00", requiredMode = Schema.RequiredMode.REQUIRED)
        private String date;

        @NotBlank(message = "活动状态不能为空")
        @Schema(description = "活动状态", example = "ongoing", allowableValues = {"ongoing", "finished"}, requiredMode = Schema.RequiredMode.REQUIRED)
        private String status;

        @Schema(description = "活动图片URL", example = "/activities/1/image")
        private String image;

        @Schema(description = "活动描述", example = "课堂练习讲解")
        private String description;

        @Schema(description = "参与学生数", example = "35")
        private Integer participantCount;

        @Schema(description = "完成率", example = "85.5")
        private Double completionRate;

        /**
         * 获取活动状态中文
         */
        @Schema(description = "活动状态中文", accessMode = Schema.AccessMode.READ_ONLY)
        public String getStatusText() {
            return "ongoing".equals(status) ? "进行中" : "已完成";
        }

        /**
         * 获取类型中文
         */
        @Schema(description = "类型中文", accessMode = Schema.AccessMode.READ_ONLY)
        public String getTypeText() {
            return "participated".equals(type) ? "参与" : "评审";
        }
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "证书信息")
    public static class CertificateDTO {

        @NotNull(message = "证书ID不能为空")
        @Positive(message = "证书ID必须是正整数")
        @Schema(description = "证书唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long id;

        @NotBlank(message = "证书标题不能为空")
        @Schema(description = "证书标题", example = "优秀教师证书", requiredMode = Schema.RequiredMode.REQUIRED)
        private String title;

        @NotBlank(message = "证书类型不能为空")
        @Schema(description = "证书类型", example = "published", allowableValues = {"published", "answered"}, requiredMode = Schema.RequiredMode.REQUIRED)
        private String type;

        @JsonFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "日期", example = "2023-11-01", requiredMode = Schema.RequiredMode.REQUIRED)
        private String date;

        @Schema(description = "证书描述", example = "表彰教学成果")
        private String description;

        @Schema(description = "回复数量（仅published类型）", example = "25")
        private Integer responses;

        @JsonFormat(pattern = "yyyy-MM-dd")
        @Schema(description = "提交日期（仅answered类型）", example = "2023-10-16")
        private String submitted;

        @Schema(description = "证书图片URL", example = "/certificates/1/image")
        private String imageUrl;

        /**
         * 获取证书类型中文
         */
        @Schema(description = "证书类型中文", accessMode = Schema.AccessMode.READ_ONLY)
        public String getTypeText() {
            return switch (type) {
                case "published" -> "发布";
                case "answered" -> "回复";
                default -> type;
            };
        }

        /**
         * 判断是否为发布类型
         */
        @Schema(description = "是否为发布类型", accessMode = Schema.AccessMode.READ_ONLY)
        public boolean isPublished() {
            return "published".equals(type);
        }
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "教师统计数据")
    public static class TeacherStatsDTO {

        @Schema(description = "所教班级数", example = "3")
        private Integer classCount;

        @Schema(description = "学生总数", example = "120")
        private Integer studentCount;

        @Schema(description = "发布作业数", example = "25")
        private Integer assignmentCount;

        @Schema(description = "平均评分", example = "4.8")
        private Double averageRating;

        @Schema(description = "教学时长（小时）", example = "180.5")
        private Double teachingHours;

        /**
         * 获取班级信息
         */
        @Schema(description = "班级信息", accessMode = Schema.AccessMode.READ_ONLY)
        public String getClassInfo() {
            return classCount + "个班级，" + studentCount + "名学生";
        }
    }
}
